require 'unveil/error'
require 'unveil/core_ext'

module Unveil
  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def unveil(*args)
      name = args[0].is_a?(Symbol) ? args.shift : :default
      self.unveil_scopes[name] = args[0] || {}
    end

    def unveil_scopes
      @unveil_scopes ||= {:default => {}}
    end
  end

  def unveil(*args)
    name = args[0].is_a?(Symbol) ? args.shift : :default
    options = args[0] || {}

    unless self.class.unveil_scopes[name]
      raise Unveil::UndefinedScopeError,
        ":#{name} hasn't been defined. Try adding `unveil :#{name}` to #{self.class.name}."
    end

    return serializable_hash(self.class.unveil_scopes[name].merge(options))
  end
end

ActiveRecord::Base.send :include, Unveil
